package StudentApply;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
@WebServlet("/SSR")
public class StudentSubmitRequest extends HttpServlet{
    //接收来自前端提交的数据
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String title=request.getParameter("title");
        String type=request.getParameter("type");
        String sno=request.getParameter("sno");
        String sname=request.getParameter("sname");
        String student_major=request.getParameter("student_major");
        String student_phone=request.getParameter("student_phone");
        DBUtil db=new DBUtil();
        //连接数据库并插入数据
        try(Connection conn=db.getConnection()){
            String sql="insert into Javaweb.javaweb十人项目组.Student(title,type,sno,sname,student_major,student_phone,request_status)" +
                    "values(?,?,?,?,?,?,?)";
            try(PreparedStatement pstmt=conn.prepareStatement(sql)){
                pstmt.setString(1, title);
                pstmt.setString(2, type);
                pstmt.setString(3, sno);
                pstmt.setString(4, sname);
                pstmt.setString(5, student_major);
                pstmt.setString(6, student_phone);
                pstmt.setString(7, "待审核");  // 默认状态为"待审核"
                int rows=pstmt.executeUpdate();
                if(rows>0){
                    //提交成功，重定向到成功页面
                    response.sendRedirect("jsp/submit_success.html");
                }else{
                    //插入失败，重定向到失败页面
                    response.sendRedirect("jsp/submit_error.html");
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();//打印异常信息
            throw new RuntimeException(e);
        }
    }
}
